package com.bana.fvs.app.enter;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.PageBaseParams;
import com.bana.fvs.app.main.page.MainActivity;
import com.bana.fvs.app.module.login.page.LoginActivity;
import com.bana.fvs.app.mvp.contract.NewVersionContract;
import com.bana.fvs.app.mvp.presenter.NewVersionPresenter;
import com.bana.fvs.app.util.DialogManager;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.dialog.BaseDialog;
import com.wei.commonlibrary.dialog.DownLoadManager;
import com.wei.commonlibrary.dialog.VersionUpdateDialog;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.AppUtils;
import com.wei.commonlibrary.utils.ToastUtil;

import java.io.File;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/2/6
 * Time   : 14:02
 * Desc   : 启动页
 */

public class SplashActivity extends BaseActivity<NewVersionPresenter> implements NewVersionContract.INewVersionView {
    private static final String TAG = "dah_SplashActivity";
    private SplashHandler mSplashHandler;
    private int what;
    private boolean isDownload;//是否下载

    @BindView(R.id.tv_app_name)
    TextView tvAppName;

    public static class Params extends PageBaseParams {
        private static final int WHAT_LOGIN = 1;
        private static final int WHAT_MAIN = 2;
    }

    @Override
    public int setLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setBaseTitleBarViewGone();
    }

    @Override
    public void initWidget() {
        tvAppName.setText(String.valueOf(getString(R.string.module_login_title)) + "V" + AppUtils.getVersionName(this));

        mSplashHandler = new SplashHandler();
        String token = UserInfoUtils.getInstance().getToken(mContext);
        String versionName = AppUtils.getVersionName(mContext);
//        mPresenter.http_getNewVersion(this, token, versionName);
        if (UserInfoUtils.getInstance().isLogin(this)) {
            what = Params.WHAT_MAIN;
        } else {
            what = Params.WHAT_LOGIN;
        }
        mSplashHandler.sendEmptyMessageDelayed(what, 2000);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (updateDialog == null && !isDownload) {
            mSplashHandler.sendEmptyMessageDelayed(what, 700);

        }
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
    }

    @Override
    public void onResponse(NewVersionBean newVersionBean) {
        if (newVersionBean != null) {
            boolean hasNewVersion = newVersionBean.isHasNewVersion();
            //版本更新
            if (hasNewVersion) {
                mSplashHandler.removeCallbacksAndMessages(null);//取消所有消息
                mSplashHandler.removeMessages(what);
                showDescDialog(newVersionBean);
            }
        }
    }

    /**
     * 检测是否需要升级新版本
     */
    private void chechUpdate() {
        showDescDialog(null);
    }

    VersionUpdateDialog updateDialog = null;

    private void showDescDialog(final NewVersionBean newVersionBean) {
        BaseDialog.OnDialogListener dialogListener = new BaseDialog.OnDialogListener() {
            @Override
            public void rightClick(String text) {
                //取消
                if (updateDialog != null) {
                    updateDialog.dismiss();
                    updateDialog = null;
                    isDownload = false;
                    mSplashHandler.sendEmptyMessageDelayed(what, 700);
                }
            }

            @Override
            public void leftClick() {
                //更新-->开始下载文件，并切换loading
                isDownload = true;
                if (updateDialog != null) {
                    updateDialog.dismiss();
                    updateDialog = null;
                }
                //更新
                DownLoadManager.startDownload((SplashActivity) mContext, newVersionBean.getUrl(), new DownLoadManager.OnDownloadListener() {
                    @Override
                    public void onDownSuccess(File apkFile) {
                        //安装
                        isDownload = false;
                        AppUtils.installApk((SplashActivity) mContext, apkFile);
                    }

                    @Override
                    public void onCancel() {
                        mSplashHandler.sendEmptyMessageDelayed(what, 700);
                    }

                    @Override
                    public void onError(CommException exception) {
                        ToastUtil.showShort(mContext, exception.getMessage());
                        mSplashHandler.sendEmptyMessageDelayed(what, 700);
                    }
                });
            }
        };
        updateDialog = DialogManager
                .createUpdateDialog(this, newVersionBean, dialogListener);
        updateDialog.show();
    }

    private class SplashHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Params.WHAT_LOGIN:
                    startActivityAndFinish(LoginActivity.class);
                    //startActivityAndFinish(SetBaseUrlActivity.class);
                    break;
                case Params.WHAT_MAIN:
                    startActivityAndFinish(MainActivity.class);
                    break;
                default:
                    break;
            }
        }
    }
}
